package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsFootprint;
import com.qf.service.AdminFootprintService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/footprint")
@CrossOrigin
@RestController
public class AdminFootprintController {

    @Autowired
    private AdminFootprintService adminFootprintservice;

    /**
     * 会员足迹
     * @param page
     * @param limit
     * @param userId
     * @param goodsId
     * @param sort
     * @param order
     * @return
     *
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"total": 5397,
     * 		"items": [
     * 			{
     * 				"id": 5397,
     * 				"userId": 164,
     * 				"goodsId": 17412,
     * 				"addTime": "2019-12-21 09:04:43",
     * 				"updateTime": "2019-12-21 09:04:43",
     * 				"deleted": false
     * 			},
     * 			{
     * 				"id": 5396,
     * 				"userId": 164,
     * 				"goodsId": 21328,
     * 				"addTime": "2019-12-21 09:04:31",
     * 				"updateTime": "2019-12-21 09:04:31",
     * 				"deleted": false
     * 			}
     * 		]
     * 	},
     * 	"errmsg": "成功"
     * }
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:footprint:list")
    @RequiresPermissionsDesc(menu = "会员足迹",button = "会员足迹列表")
    public Object footprintList(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "20") Integer limit,
                                Integer userId,Integer goodsId,
                                String sort,String order){

        IPage<DtsFootprint> iPage = adminFootprintservice.selectFootprintList(page,limit,userId,goodsId,sort,order);

        long total = iPage.getTotal();
        List<DtsFootprint> records = iPage.getRecords();

        DtsFootprint[] items = new DtsFootprint[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }


}
